<?php
namespace app\index\controller;

//求购二货
class Seeksecondhand extends Base
{

    /**
     * 发布 求购信息
     */
    public function publishSeek()
    {
        $data = input('post.');

        if (!isset($data['sk_uid']) || empty($data['sk_uid'])) {
            $info['code']     = '401';
            $info['message']  = '数据错误，请重新登录';
            echo displayJson($info);die;
        }

        $user_info = db('user',[],false)->where('us_id',$data['sk_uid'])->find();

        if (!$user_info['us_school']) {
            $info['code']     = '400';
            $info['message']  = '请先前往个人中心完善学校等信息';
            echo displayJson($info);die;
        }

        //校验数据
        $validate = \think\Loader::validate('Seek');
        if(!$validate->check($data)){
            $info = array('code' => '400','message'=>$validate->getError());
            echo displayJson($info);die;
        }

        $data['sk_title'] = htmlspecialchars($data['sk_title']);
        $data['sk_desc']  = htmlspecialchars($data['sk_desc']);

        $data['sk_school']       = $user_info['us_school'];

        $data['sk_create_time']  = time();

        $res = db('seek',[],false)->insert($data);

        if ($res) {
            $info['code']     = '200';
            $info['message']  = '发布成功';
            echo displayJson($info);die;
        } else {
            $info['code']     = '400';
            $info['message']  = '发布失败';
            echo displayJson($info);die;
        }
    }

    /**
     * 查看我发布的求购信息
     */
    public function getMySeek()
    {
        $id   = input('us_id');

        $list = db('seek',[],false)->where('sk_uid',$id)->where('sk_status',1)->order('sk_create_time DESC')->select();

        $info['code']     = '200';
        $info['data']     = $list;
        echo displayJson($info);die;
    }

    /**
     * 修改求购信息为 已找到
     */
    public function changeSeek()
    {
        $uid = input('us_id');
        $sid = input('sk_id');

        if (empty($uid) || empty($sid)) {
            $info['code']     = '401';
            $info['message']  = '数据异常，请重新登录';
            echo displayJson($info);die;
        }

        $res = db('seek',[],false)->where('sk_id',$sid)->where('sk_uid',$uid)->where('sk_status',1)->setField('sk_condition','2');

        if ($res) {
            $info['code']     = '200';
            $info['message']  = '修改成功';
            echo displayJson($info);die;
        } else {
            $info['code']     = '400';
            $info['message']  = '修改失败';
            echo displayJson($info);die;
        }
    }

    /**
     * 重新上架
     */
    public function againSeek()
    {
        $sid = input('sid');//求购id
        $uid = input('uid');//用户id

        if (!$sid || !$uid) {
            $info['code']     = '401';
            $info['message']  = '异常请求,请重新登录';
            echo displayJson($info);die;
        }

        $row = db('seek',[],false)
            ->where('sk_id',$sid)
            ->where('sk_uid',$uid)
            ->where('sk_condition',1)
            ->where('sk_status',1)
            ->find();

        if (!$row) {
            $info['code']     = '400';
            $info['message']  = '求购信息状态异常，请刷新后重试';
            echo displayJson($info);die;
        }

        if ($row['sk_overdue_time'] > time()) {
            $info['code']     = '400';
            $info['message']  = '该求购未过期，不可重新上架';
            echo displayJson($info);die;
        }

        $set['sk_create_time']  = time();

        $set['sk_overdue_time'] = time() + __PASS_TIME__;
        $res = db('seek',[],false)->where('sk_id',$sid)->where('sk_uid',$uid)->update($set);

        if ($res) {
            $info['code']     = '200';
            $info['message']  = '重新发布成功';
            echo displayJson($info);die;
        } else {
            $info['code']     = '400';
            $info['message']  = '重新发布失败';
            echo displayJson($info);die;
        }
    }

    /**
     * 删除求购信息
     */
    public function delSeek()
    {
        $uid = input('us_id');
        $sid = input('sk_id');

        if (empty($uid) || empty($sid)) {
            $info['code']     = '401';
            $info['message']  = '数据异常，请重新登录';
            echo displayJson($info);die;
        }

        $res = db('seek',[],false)->where('sk_id',$sid)->where('sk_uid',$uid)->where('sk_status',1)->setField('sk_status','2');

        if ($res) {
            $info['code']     = '200';
            $info['message']  = '删除成功';
            echo displayJson($info);die;
        } else {
            $info['code']     = '400';
            $info['message']  = '删除失败';
            echo displayJson($info);die;
        }
    }




}